package com.jplus.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.jplus.enums.ResultEnum;
import com.jplus.exception.SFUmbrellaException;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import sun.rmi.runtime.Log;

/**
 * 使用滴秒的短信发送服务
 */
@Slf4j
public class SendAuthCodeUtils {
	private static final String QUERY_PATH="https://api.miaodiyun.com/20150822/industrySMS/sendSMS";
	private static final String ACCOUNT_SID="40be84e3bec04bc0af1a2cc808dad9dd";
	private static final String AUTH_TOKEN="4ede6bf4576845908145158e5defd775";
	
	
	//根据相应的手机号发送验证码
	public static String send(String phone, String authCode){
		String rod=authCode;
		String timestamp=getTimestamp();
		String sig=getMD5(ACCOUNT_SID,AUTH_TOKEN,timestamp);
		String tamp="【顺风伞】登录验证码："+rod+"，如非本人操作，请忽略此短信。";
		OutputStreamWriter out=null;
		BufferedReader br=null;
		StringBuilder result=new StringBuilder();
		try {
			URL url=new URL(QUERY_PATH);
			HttpURLConnection connection=(HttpURLConnection) url.openConnection();
			connection.setRequestMethod("POST");
			connection.setDoInput(true);//设置是否允许数据写入
			connection.setDoOutput(true);//设置是否允许参数数据输出
			connection.setConnectTimeout(5000);//设置链接响应时间
			connection.setReadTimeout(10000);//设置参数读取时间
			connection.setRequestProperty("Content-type","application/x-www-form-urlencoded");			
			//提交请求
			out=new OutputStreamWriter(connection.getOutputStream(),"UTF-8");
			String args=getQueryArgs(ACCOUNT_SID, tamp, phone, timestamp, sig, "JSON");
			out.write(args);
			out.flush();
			//读取返回参数
			
			br=new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
			String temp="";
			while((temp=br.readLine())!=null){
				result.append(temp);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JSONObject json=new JSONObject(result.toString());
		/*
		 默认respCode返回的是00000，表示短信发送成功
		 若返回00141，表示一小时内对同一个手机号发送短信超过4次的限制
		 具体API请看 http://www.miaodiyun.com/doc/status_code.html
		  */
		String respCode=json.getString("respCode");
		if (respCode.equals("00000")){
		     //返回输入的验证码
			 return rod;
		} else {
		    // 发送失败，抛异常。
            System.out.println("短信发送失败，错误代码：" + respCode);
            log.error("短信发送失败，错误代码：" + respCode);
		    throw new SFUmbrellaException(ResultEnum.REGISTER_FAIL);
		}
	}
	//定义一个请求参数拼接方法
	public static String getQueryArgs(String accountSid,String smsContent,String to,String timestamp,String sig,String respDataType){
		return "accountSid="+accountSid+"&smsContent="+smsContent+"&to="+to+"&timestamp="+timestamp+"&sig="+sig+"&respDataType="+respDataType;
	}
	//获取时间戳
	public static String getTimestamp(){
		return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
	}
	//sing签名
	public static String getMD5(String sid,String token,String timestamp){
		
		StringBuilder result=new StringBuilder();
		String source=sid+token+timestamp;
		//获取某个类的实例
				try {					
				   MessageDigest digest=MessageDigest.getInstance("MD5");
				   //要进行加密的东西
				   byte[] bytes=digest.digest(source.getBytes());
				   for(byte b:bytes){
					   String hex=Integer.toHexString(b&0xff);
					   if(hex.length()==1){
						   result.append("0"+hex);
					   }else{
						   result.append(hex);
					   }
				   }
				} catch (NoSuchAlgorithmException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
				
		
		return result.toString();
	}
	//创建验证码
//	public static String smsCode(){
//		String random=(int)((Math.random()*9+1)*100000)+"";
//		return random;
//	}
}
